package priv.lhy.user.services;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import priv.lhy.constants.ResponseCodeEnum;
import priv.lhy.constants.ServiceException;
import priv.lhy.dto.UserQueryRequest;
import priv.lhy.dto.UserQueryResponse;
import priv.lhy.user.IUserQueryService;
import priv.lhy.user.dal.entity.User;
import priv.lhy.user.dal.persistence.UserMapper;
import priv.lhy.user.exception.ExceptionUtil;

/**
 * author : lihy
 * date : 2019/2/15 13:24
 */
@Service("userQueryService")
public class UserQueryServiceImpl implements IUserQueryService {
    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    UserMapper userMapper;

    @Override
    public UserQueryResponse getUserById(UserQueryRequest request) {
        logger.info("login request:" + request);
        UserQueryResponse response = new UserQueryResponse();
        User user = userMapper.getUserByUid(request.getUid());
        try {
            if (user == null) {
                response.setCode(ResponseCodeEnum.USERORPASSWORD_ERROR.getCode());
                response.setMsg(ResponseCodeEnum.USERORPASSWORD_ERROR.getMsg());
                return response;
            }
            response.setCode(ResponseCodeEnum.SUCCESS.getCode());
            response.setMsg(ResponseCodeEnum.SUCCESS.getMsg());
            response.setAvatar(user.getAvatar());
            response.setMobile(user.getMobile());
            response.setRealName(user.getRealname());
            response.setSex(user.getSex());
        } catch (Exception e) {
            logger.error("userQuery occur exception :" + e);
            ServiceException serviceException = (ServiceException) ExceptionUtil.handlerException4biz(e);
            response.setCode(serviceException.getErrorCode());
            response.setMsg(serviceException.getErrorMessage());
        } finally {
            logger.info("userQuery response->" + response);
        }
        return response;
    }
}
